package com.atguigu.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.client.ProductFeignClient;
import com.atguigu.client.SearchFeignClient;
import com.atguigu.entity.BaseCategoryView;
import com.atguigu.entity.ProductSalePropertyKey;
import com.atguigu.entity.SkuInfo;
import com.atguigu.executor.MyExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Controller
public class WebSkuDetailController {
    @Resource
    private ProductFeignClient productFeignClient;
    @Resource
    private SearchFeignClient searchFeignClient;
    @RequestMapping("/{skuId}.html")
    public String getSkuDetail(@PathVariable Long skuId, Model model){
        Map<String, Object> map = new HashMap<>();
        //利用CompletableFuture实现异步编排(异步请求)
        CompletableFuture<SkuInfo> skuInfoAsync = CompletableFuture.supplyAsync(() -> {
            //根据skuid查询skuInfo信息
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            map.put("skuInfo",skuInfo);
            return skuInfo;
        }, MyExecutor.getInstance());

        CompletableFuture<Void> spuSalePropertyListAsync = skuInfoAsync.thenAcceptAsync(skuInfo -> {
            //根据productId skuId 获取商品的销售属性
            Long productId = skuInfo.getProductId();
            List<ProductSalePropertyKey> spuSalePropertyList = productFeignClient.getSpuSaleProperty(productId, skuId);
            map.put("spuSalePropertyList", spuSalePropertyList);
        },MyExecutor.getInstance());

        CompletableFuture<Void> priceAsync = CompletableFuture.runAsync(() -> {
            //根据skuId查询商品实时价格
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            map.put("price", skuPrice);
        },MyExecutor.getInstance());

        CompletableFuture<Void> categoryViewAsync = skuInfoAsync.thenAcceptAsync(skuInfo -> {
            //根据skuId查询分类信息
            Long category3Id = skuInfo.getCategory3Id();
            BaseCategoryView categoryView = productFeignClient.getCategoryView(category3Id);
            map.put("categoryView", categoryView);
        },MyExecutor.getInstance());

        CompletableFuture<Void> salePropertyValueIdJsonAsync = skuInfoAsync.thenAcceptAsync(skuInfo -> {
            //通过spuId查询spu的商品属性 与 skuId获取对应的商品属性 的关系
            Map propertyValueIdMap = productFeignClient.getSkuSalePropertyValueId(skuInfo.getProductId());
            //将map数据 转为JSON字符串 格式
            String salePropertyValueIdJson = JSON.toJSONString(propertyValueIdMap);
            map.put("salePropertyValueIdJson", salePropertyValueIdJson);
        },MyExecutor.getInstance());

        CompletableFuture<Void> incrHotScoreAsync = CompletableFuture.runAsync(() -> {
            //商品的热度排名
            searchFeignClient.incrHotScore(skuId);
        }, MyExecutor.getInstance());

        //将异步获取的所有数据 统一返回
        CompletableFuture.allOf(
                skuInfoAsync,
                spuSalePropertyListAsync,
                priceAsync,
                categoryViewAsync,
                salePropertyValueIdJsonAsync,
                incrHotScoreAsync
                ).join();

        model.addAllAttributes(map);
        return "detail/index";
    }
}
